<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script th:src="@{/jquery-3.6.0.min.js}"></script>

</head>

<body>
<h1>授权</h1>
<h3>用户名:[[${user.userName}]]</h3>
已经选择的菜单:<br/>
<h4 style="color: red" th:each="selected:${selectedMenus}" th:text="${selected.menuName}"></h4>

<input type="hidden" id="userId" th:value="${user.id}">
所有的菜单:<br/>
<select multiple id="menuIds">
   <option th:each="menu:${menus}" th:text="${menu.menuName}" th:value="${menu.id}" ></option>
</select><br/>

<button id="auth">授权</button>
</body>
<script>
    jQuery(function(){
        $('#auth').click(function(){
            debugger
            var userId = $('#userId').val()
            var menuIds = $('#menuIds').val()
            var data = {userId:userId,menuIds:menuIds}

            $.ajax({
                url:'/user/doAuth',
                type:'post',
                contentType:'application/json',
                data:JSON.stringify(data),
                success:function(res){
                    if(res == 'success'){
                        window.location.href='/user/list'
                    }else{
                        alert('授权失败！')
                    }
                }
            })
        })
    })
</script>
</html>